package axdoc.service.impl;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.util.excel.ExcelUtil;

import axdoc.dao.TagsDao;
import axdoc.model.tags.ExcelModel;
import axdoc.model.tags.TagsContent;
import axdoc.model.tags.TagsDisease;
import axdoc.model.tags.TagsDiseaseDept;
import axdoc.model.tags.TagsDiseaseIllness;
import axdoc.model.tags.TagsIllness;
import axdoc.model.tags.TagsIllnessSymptom;
import axdoc.model.tags.TagsSymptom;
import axdoc.service.TagsService;
import axdoc.util.StringUtils;

@Service
public class TagsServiceImpl implements TagsService {

	@Autowired
	TagsDao tagsDao;

	@Autowired
	JdbcTemplate jdbcTemplate;
	@Override
	public void parseUserTags() {
		List<Object> list = new ArrayList<Object>();
		String path1 = "/Users/wangxh/Downloads/tags/sheet05.xlsx";
		List<Object> list1 = ExcelUtil.getInstance().readExcel2ObjsByPath(path1, ExcelModel.class);
		list.addAll(list1);
		if (list != null && list.size() > 0) {
			Map<String, Integer> diseaseMap = new HashMap<String, Integer>();
			Map<String, Integer> deptMap = new HashMap<String, Integer>();
			int i = 0;
			for (Object o : list) {
				i++;
				System.out.println(i + "==" + o.toString());

				ExcelModel em = (ExcelModel) o;

				// 新增病种
				Integer diseaseId = diseaseMap.get(em.getDisease().trim());
				if (diseaseId == null) {
					TagsDisease tc = new TagsDisease();
					tc.setDiseaseName(em.getDisease().trim());
					diseaseId = tagsDao.addDisease(tc);
					diseaseMap.put(em.getDisease().trim(), diseaseId);
					System.out.println("disId=" + diseaseId);
				}

				// 新增病种科室关联
				String[] depts = em.getDept().trim().replace("，", ",").split(",");
				for (String dept : depts) {
					Integer deptId = deptMap.get(dept.trim() + diseaseId);
					if (deptId == null) {
						TagsDiseaseDept tdd = new TagsDiseaseDept();
						tdd.setDiseaseId(diseaseId);
						tdd.setDept(dept);
						deptId = tagsDao.addDiseaseDept(tdd);
						deptMap.put(dept.trim() + diseaseId, deptId);
						System.out.println("deptId=" + deptId);
					}
				}

				TagsIllness ti = new TagsIllness();
				ti.setIllnessName(em.getIllness().trim());
				ti.setIllnessDesc(em.getDesc().trim());
				int illnessId = tagsDao.addIllness(ti);
				System.out.println("illnessId=" + illnessId);

				TagsDiseaseIllness tdi = new TagsDiseaseIllness();
				tdi.setDiseaseId(diseaseId);
				tdi.setIllnessId(illnessId);
				int disIllId = tagsDao.addDiseaseIllness(tdi);
				System.out.println("disIllId=" + disIllId);

				String[] syms = em.getSyms().trim().replace("，", ",").split(",");
				if (syms != null && syms.length > 0) {
					for (int j = 0; j < syms.length; j++) {
						if (StringUtils.isNotBlank(syms[j])) {
							TagsSymptom ts = new TagsSymptom();
							ts.setSymptomName(syms[j].trim());
							int symId = tagsDao.addSymptom(ts);
							System.out.println("symId=" + symId);

							TagsIllnessSymptom tis = new TagsIllnessSymptom();
							tis.setIllnessId(illnessId);
							tis.setSymptomId(symId);
							int illSymId = tagsDao.addIllnessSymptom(tis);
							System.out.println("illSymId=" + illSymId);
						}
					}
				}

			}
		}
	}

	@Override
	public void parseDoctorTags() {

	}

	@Override
	public void parseContentTags() {
		List<Object> list = new ArrayList<Object>();
		String path1 = "/Users/wangxh/Downloads/tags/sheet13.xlsx";
		List<Object> list1 = ExcelUtil.getInstance().readExcel2ObjsByPath(path1, TagsContent.class);
		list.addAll(list1);
		if (list != null && list.size() > 0) {
			int i = 0;
			for (Object o : list) {
				i++;
				System.out.println(i + "==" + o.toString());
				tagsDao.addAct((TagsContent) o);
			}
		}
	}

	@Override
	public TagsDisease queryDisease(String diseaseName) {
		return tagsDao.queryDisease(diseaseName);
	}

	@Override
	public void parseDoctorSkilled() {
		String sql = "select * from disease where 1=2";
		List<Object> list= new ArrayList<Object>();
		list=jdbcTemplate.query(sql, new RowMapper<Object>() {
			@Override
			public Map<String, Object> mapRow(ResultSet arg0, int arg1) throws SQLException {
				 Map<String, Object> m=new HashMap<String,Object>();
				ResultSetMetaData md = arg0.getMetaData();//获取键名
				int columnCount = md.getColumnCount();//获取行的数量
				
				for (int i = 1; i <= columnCount; i++) {
					m.put(md.getColumnName(i), arg0.getObject(i));
				}
				
				return m;
			}
		});
		
		System.out.println(list);
	}
	
}


